package com.shangcheng.search.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.google.inject.internal.asm.$Type;
import com.shangcheng.goods.feign.BrandFeign;
import com.shangcheng.goods.feign.SpuFeign;
import com.shangcheng.goods.pojo.Sku;
import com.shangcheng.users.feign.UserFeign;
import com.shangcheng.users.pojo.User;
import entity.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.*;


@Controller
@RequestMapping(value = "/")
public class UsersController {
    @Autowired
    private UserFeign userFeign;
    @Autowired
    private SpuFeign spuFeign;
    @Autowired
    private BrandFeign brandFeign;

    @GetMapping(value = "/register")
    public String register() {
        return "register";
    }

    @GetMapping(value = "/login")
    public String login() {
        return "login";
    }

    @PostMapping(value = "/dL")
    public String dL(@RequestParam String username,
                     @RequestParam String password, ModelMap model) {
        Result<User> user = userFeign.auth(username, password);
        if (user.getCode() == 20002) {
            model.addAttribute("user", user.getMessage());
            return "login";
        } else {
            List list = new ArrayList();
            list.add(user.getData());
            String jsonList = JSON.toJSONString(list);
            List<User> list1 = JSON.parseArray(jsonList, User.class);
            User users = list1.get(0);
            LinkedHashMap hashMap = new LinkedHashMap();
            hashMap.put("username", users);
            model.addAttribute("username", username);
            Result brandsR = brandFeign.findAll();
            Result<PageInfo> spuPages = spuFeign.goodsList(1, 20);
            model.addAttribute("brandList", brandsR.getData());
            //商品列表数据
            model.addAttribute("goods", spuPages.getData().getList());
            //size
            model.addAttribute("pageSize", spuPages.getData().getPageSize());
            ///上一页
            model.addAttribute("prePage", spuPages.getData().getPrePage());
            //第一页
            model.addAttribute("firstPage", spuPages.getData().getFirstPage());
            //最后一页
            model.addAttribute("lastPage", spuPages.getData().getLastPage());
            //下一页
            model.addAttribute("nextPage", spuPages.getData().getNextPage());
            return "indexsu";
        }

    }

    @GetMapping(value = "/p")
    public String GoodsList(@RequestParam int page,
                            @RequestParam int size,
                            @RequestParam String username,
                            ModelMap model) {
        Result brandsR = brandFeign.findAll();
        Result<PageInfo> spuPages = spuFeign.goodsList(page, size);
        model.addAttribute("brandList", brandsR.getData());
        //商品列表数据
        model.addAttribute("username", username);
        model.addAttribute("goods", spuPages.getData().getList());
        //size
        model.addAttribute("pageSize", spuPages.getData().getPageSize());
        ///上一页
        model.addAttribute("prePage", spuPages.getData().getPrePage());
        //第一页
        model.addAttribute("firstPage", spuPages.getData().getFirstPage());
        //最后一页
        model.addAttribute("lastPage", spuPages.getData().getLastPage());
        //下一页
        model.addAttribute("nextPage", spuPages.getData().getNextPage());
        return "indexsu";
    }

    @PostMapping(value = "/zc")
    public String zc(@RequestParam String phone,
                     @RequestParam String yzm,
                     @RequestParam String password,
                     @RequestParam String passwordtwo,
                     @RequestParam String m1,
                     ModelMap model) {
        User user = new User();
        user.setUsername(phone);
        user.setPhone(phone);
        Date date = new Date();
        user.setCreated(date);
        user.setUpdated(date);
        if (password.equals(passwordtwo)) {
            user.setPassword(password);
        } else {
            model.addAttribute("userError", "密码不一致");
            return "register";
        }
        Result users = userFeign.add(user);
        if (users.getCode() == 20002) {
            model.addAttribute("userError", "注册失败");
            return "register";
        } else {
            model.addAttribute("user", "注册成功!");
            return "login";
        }
    }

}
